/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file FormSessionInfo.cpp
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include "forms/FormSessionInfo.h"
#include "SCFormManager.h"
#include "models/SCHelpers.h"
#include "SCTranslator.h"

FormSessionInfo::FormSessionInfo()
{
}

FormSessionInfo::~FormSessionInfo()
{
}

bool
FormSessionInfo::Initialize()
{
	Construct(L"IDF_SESSION_INFO");
	return true;
}

result
FormSessionInfo::OnInitializing(void)
{
	result r = E_SUCCESS;

	//init Graphics
	this->pScrollPanel = static_cast<ScrollPanel*>(GetControl("IDC_SCROLLPANEL_INFO"));
	this->pStartEF = static_cast<EditField*>(this->pScrollPanel->GetControl("IDC_EDITFIELD_START"));
	this->pEndEF = static_cast<EditField*>(this->pScrollPanel->GetControl("IDC_EDITFIELD_END"));
	this->pStopEF = static_cast<EditField*>(this->pScrollPanel->GetControl("IDC_EDITFIELD_STOP"));
	this->pQualityEF = static_cast<EditField*>(this->pScrollPanel->GetControl("IDC_EDITFIELD_QUALITY"));
	this->pSamplesEF = static_cast<EditField*>(this->pScrollPanel->GetControl("IDC_EDITFIELD_SAMPLES"));

	pStartEF->SetFocusable(false);
	pStopEF->SetFocusable(false);
	pEndEF->SetFocusable(false);
	pQualityEF->SetFocusable(false);
	pSamplesEF->SetFocusable(false);

	//language
	this->SetTitleText(SCTranslator::GetText("IDS_SESSIONINFO_TITLE"));
	this->pStartEF->SetTitleText(SCTranslator::GetText("IDS_SESSIONINFO_START"));
	this->pEndEF->SetTitleText(SCTranslator::GetText("IDS_SESSIONINFO_END"));
	this->pStopEF->SetTitleText(SCTranslator::GetText("IDS_SESSIONINFO_STOP"));
	this->pQualityEF->SetTitleText(SCTranslator::GetText("IDS_SESSIONINFO_QUALITY"));
	this->pSamplesEF->SetTitleText(SCTranslator::GetText("IDS_SESSIONINFO_SAMPLES"));

	//init footer
	Footer* pFooter = GetFooter();
	pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
	pFooter->AddActionEventListener(*this);

	FooterItem* pFooterBackItem = new FooterItem();
	pFooterBackItem->Construct(IDA_SESSION_INFO_BACK);
	pFooterBackItem->SetText(SCTranslator::GetText("IDS_ALARMS_BACK"));

	pFooter->AddItem(*pFooterBackItem);

	// update
	//this->Update();

	delete pFooterBackItem;


	return r;
}

result
FormSessionInfo::OnTerminating(void)
{
	result r = E_SUCCESS;
	return r;
}

void
FormSessionInfo::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch(actionId)
	{
		case IDA_SESSION_INFO_BACK:
			SCFormManager::SwitchToForm(SCFormManager::IDF_SESSIONS, false, null, SCFormManager::SLIDE_RIGHT);
			break;
	}
}

void
FormSessionInfo::SetDefaultValues()
{
	this->pStartEF->SetText("-");
	this->pEndEF->SetText("-");
	this->pStopEF->SetText("-");
	this->pQualityEF->SetText("-");
	this->pSamplesEF->SetText("-");
}

void
FormSessionInfo::Update()
{
	if(this->pSession != NULL) {
		String quality, samples;

		samples.Append(this->pSession->GetSamplesCount());

		this->pStartEF->SetText(SCHelpers::ConvertDateTimeToString(this->pSession->GetStartDate(), "dd.MM. (hh:mm)"));
		if(this->pSession->GetType() == SCSession::IDA_SESSION_TYPE_ANALYZE_AND_WAKEUP)
		{
			this->pEndEF->SetText(SCHelpers::ConvertDateTimeToString(this->pSession->GetEndDate(), "hh:mm"));
		} else {
			this->pEndEF->SetText("-");
		}

		if(this->pSession->GetQualityOfSleep() > 0)
		{
			quality.Append(this->pSession->GetQualityOfSleep());
			this->pQualityEF->SetText(quality);
		} else {
			this->pQualityEF->SetText("-");
		}

		this->pStopEF->SetText(SCHelpers::ConvertDateTimeToString(this->pSession->GetStopDate(), "dd.MM. (hh:mm)"));
		this->pSamplesEF->SetText(samples);
	}
}

void
FormSessionInfo::Redraw()
{
	this->Update();
	RequestRedraw(true);
}

int
FormSessionInfo::GetFormId()
{
	return SCFormManager::IDF_SESSIONS_INFO;
}

void
FormSessionInfo::SetSession(SCSession* pSession)
{
	this->pSession = pSession;
}

SCSession*
FormSessionInfo::GetSession()
{
	return this->pSession;
}
